package com.hakim.wfb.customer.service.impl;

import com.hakim.wfb.bean.PurchaseList;
import com.hakim.wfb.customer.params.goods.CheckUserInvestingParameter;
import com.hakim.wfb.customer.result.goods.CheckUserInvestingView;
import com.hakim.wfb.customer.result.goods.GoodsListView;
import com.hakim.wfb.customer.service.GoodsService;
import com.hakim.wfb.dao.GoodsListDao;
import com.hakim.wfb.dao.PurchaseListDao;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hakim.wfb.common.serial.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2017/7/4.
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsListDao goodsListDao;
    @Autowired
    private PurchaseListDao purchaseListDao;

    @Override
    public ListResultEx<GoodsListView> list() {

        ListResultEx<GoodsListView> res = new ListResultEx<GoodsListView>();
        try {
            List<GoodsListView> gll = goodsListDao.selectEnabled();
            GoodsListView view = gll.get(0);
            view.setStep("2");
            view.setIntroInfo("点击下方将商品加入购物车");

            res.setTotalCount(gll.size());
            res.setDataList(gll);
            res.makeSuccessResult();
        } catch (Exception e) {
            return res.makeFailedResult("操作失败");
        }
        return res;
    }

    @Override
    public ResultEx addChart(String goodsid, String userid) {
        PurchaseList pl = new PurchaseList();
        pl.setTid(UUIDGenerator.getUUID());
        pl.setPlPhone(goodsid);
        pl.setPlUser(userid);
        pl.setPlCreateDate(new Date());
        pl.setPlUpdateDate(new Date());
        purchaseListDao.add(pl);
        return new ResultEx().makeSuccessResult();
    }


    @Override
    public ResultEx deleChart(String purchaseid, String userid) {
        PurchaseList pl = new PurchaseList();
        pl.setTid(purchaseid);
        pl.setPlUser(userid);
        purchaseListDao.deleChart(pl);
        return new ResultEx().makeSuccessResult();
    }


    /**
     * @param parameter
     * @return
     */
    @Override
    public ObjectResultEx<CheckUserInvestingView> checkInvest(CheckUserInvestingParameter parameter) {
        ObjectResultEx<CheckUserInvestingView> objectResultEx = new ObjectResultEx<>();

        try {
            CheckUserInvestingView checkUserInvestingView = new CheckUserInvestingView();
            int gll = goodsListDao.checkInvest(parameter.getUserId());
            if (gll > 0)
                checkUserInvestingView.setInvesting(true);
            objectResultEx.setDataObject(checkUserInvestingView);
            objectResultEx.makeSuccessResult();
        } catch (Exception e) {
            return objectResultEx.makeFailedResult("操作失败");
        }
        return objectResultEx;
    }
}
